<?php
require('functions/page_load.php');

$users_nav = true;

if($_SERVER['REQUEST_METHOD'] == 'POST'){

	// insert user
	
	$encrypt1 = base64_encode($_POST['password'].'_'.$encryption_salt);
	$encrypt2 = md5($encrypt1.'_'.$_POST['email'].'_'.$encryption_salt);
	
	//Change By Vyas Ishan 14 May 2013 Task 1
	$surName = '';
	if (array_key_exists('surname',$_POST)) {
		$surName = mysql_real_escape_string($_POST['surname']);
	}
	
	$sql = "INSERT INTO users SET
	firstname = '".mysql_real_escape_string($_POST['firstname'])."',
	surname = '".$surName."',
	telephone = '".mysql_real_escape_string($_POST['telephone'])."',
	mobile = '".mysql_real_escape_string($_POST['mobile'])."',
	email = '".mysql_real_escape_string($_POST['email'])."',
	password = '".mysql_real_escape_string($encrypt2)."',
	newsletter = '".mysql_real_escape_string($_POST['newsletter'])."',
	member_since = NOW(),
	tradesman = '".mysql_real_escape_string($_POST['tradesman'])."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	$new_user_id = mysql_insert_id();

	// insert address
	
		$address = $_POST['line_1'].",".$_POST['line_2'].",".$_POST['line_3'].",".$_POST['postcode'];
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, "http://maps.google.com/maps/geo?q=".urlencode($address)."&output=csv&oe=utf8&sensor=false&key=".$settings['google_api_key']);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$output = curl_exec($ch);
			curl_close($ch);
		$outputArr = explode(",",$output);
		$latitude = $outputArr[2];
		$longitude = $outputArr[3];
	
	$sql = "INSERT INTO addresses SET 
			area_id = '".mysql_real_escape_string($_POST['area_id'])."', 
			line_1 = '".mysql_real_escape_string($_POST['line_1'])."', 
			line_2 = '".mysql_real_escape_string($_POST['line_2'])."', 
			line_3 = '".mysql_real_escape_string($_POST['line_3'])."', 
			postcode = '".mysql_real_escape_string($_POST['postcode'])."',
			latitude = '".$latitude."',
			longitude = '".$longitude."',
			user_id = '".$new_user_id."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	$new_address_id = mysql_insert_id();
	
	// update user
	$sql = "UPDATE users SET address_id = '".$new_address_id."' WHERE id = '".$new_user_id."'";
	$query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
	

	header("Location: edit_user.php?new=true&id=".$new_user_id);

}

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Filters</h2>

<ul>
<li><a href="users.php">Active users</a></li>
<li><a href="users.php?show=banned">Banned users</a></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">Add User</h2>
	
	<div id="top_links">
	<a href="users.php" style="background-image: url(images/back.png); padding-left: 20px;">Back to Users</a>
	</div>
	
	<?php echo $results; ?>
	
			<form method="post" action="">
			
			<!-- user details -->
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="firstname">First name:</label></div>
			<div class="form_row_right"><input type="text" name="firstname" id="firstname" class="textbox" value="<?php echo $rs['firstname']; ?>" style="width: 520px;" /></div>
			</div>
				
			<?php /* Change By Vyas Ishan 14 May 2013 Task1?>	
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="surname">Surname:</label></div>
			<div class="form_row_right"><input type="text" name="surname" id="surname" class="textbox" value="<?php echo $rs['surname']; ?>" style="width: 520px;" /></div>
			</div>
			<?php */?>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="telephone">Telephone:</label></div>
			<div class="form_row_right"><input type="text" name="telephone" id="telephone" class="textbox" value="<?php echo $rs['telephone']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="mobile">Mobile:</label></div>
			<div class="form_row_right"><input type="text" name="mobile" id="mobile" class="textbox" value="<?php echo $rs['mobile']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="email">Email:</label></div>
			<div class="form_row_right"><input type="text" name="email" id="email" class="textbox" value="<?php echo $rs['email']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="password">Password:</label></div>
			<div class="form_row_right"><input type="text" name="password" id="password" class="textbox" value="<?php echo $rs['password']; ?>" style="width: 520px;" /></div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="newsletter">Newsletter:</label></div>
			<div class="form_row_right"><input type="checkbox" name="newsletter" id="newsletter" value="1"<?php echo ($rs['newsletter'] == '1') ? ' checked="checked"' : '' ; ?> /></div>
			</div>
			
			<!-- address details -->
			
			<div class="form_row_wrapper">
			<div class="form_row_left"><label for="line_1">Address:</label></div>
			<div class="form_row_right">
			<input type="text" name="line_1" id="line_1" class="textbox" value="" />
			<input type="text" name="line_2" id="line_2" class="textbox" value="" />
			<input type="text" name="line_3" id="line_3" class="textbox" value="" />
			<select name="area_id" id="area_id" style="margin-top: 5px;" class="textbox clearLeft">
			<?php
			
			$sql = "SELECT id, area FROM areas ORDER BY area";
			$area_query = mysql_query( $sql ) or die ("Error in query: $sql. " . mysql_error());
			while($area_rs = mysql_fetch_assoc($area_query)){
			
			echo '<option value="'.$area_rs['id'].'"'.$selected.'>'.$area_rs['area'].'</option>';
			
			}
			
			?></select>
			<input type="text" name="postcode" id="postcode" class="textbox clearLeft" value="<?php echo $rs['postcode']; ?>" style="width: 80px;" />
			</div>
			</div>
			
			<div class="form_row_wrapper">
			<div class="form_row_left">&nbsp;</div>
			<div class="form_row_right"><input type="image" src="images/save_button.png" alt="Save" /></div>
			</div>
			
			</form>
		
	</div>

</div>
	
<?php
	require('includes/footer.php');
?>